{extend name="common/main"/}

{block name="body"}
    <div class="main-title">
        <h2>数据备份</h2>
    </div>

    <div class="with-padding">

        <div class="tool-box button-box">
            <a id="export" class="btn btn-info" href="javascript:;" autocomplete="off">立即备份</a>
            <a id="optimize" class="btn btn-warning" href="{:url('optimize')}">优化表</a>
            <a id="repair" class="btn btn-warning" href="{:url('repair')}">修复表</a>
        </div>
        <form id="export-form" method="post" action="{:url('export')}">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th width="60">
                            <input type="checkbox" checked="chedked" class="check-all" id="check-all">
                            <label for="check-all"></label>
                        </th>
                        <th>表名</th>
                        <th width="120">数据量</th>
                        <th width="120">数据大小</th>
                        <th width="160">创建时间</th>
                        <th width="160">备份状态</th>
                        <th width="120">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {volist name="list" id="table"}
                        <tr>
                            <td class="num">
                                <input class="ids" id="table-{$table.name}" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                                <label for="table-{$table.name}"></label>
                            </td>
                            <td>{$table.name}</td>
                            <td>{$table.rows}</td>
                            <td>{$table.data_length|format_bytes}</td>
                            <td>{$table.create_time}</td>
                            <td class="info">未备份</td>
                            <td class="action">
                                <a class="ajax-get no-refresh" href="{:url('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                                <a class="ajax-get no-refresh" href="{:url('repair?tables='.$table['name'])}">修复表</a>
                            </td>
                        </tr>
                    {/volist}
                </tbody>
            </table>
        </form>
    </div>
{/block}

{block name="script"}
<script type="text/javascript">
    (function($){
        var $form = $("#export-form"), 
            $export = $("#export"), tables
            $optimize = $("#optimize"), 
            $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.code){
                    updateAlert(data.msg,'success');
                } else {
                    updateAlert(data.msg,'error');
                }
                setTimeout(function(){
	                $('#top-alert').find('button').click();
	                $(that).removeClass('disabled').prop('disabled',false);
	            },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post($form.attr("action"), $form.serialize(), function(res){
                if(res.code == 200){
                    tables = res.data.tables;
                    $export.html(res.msg + '开始备份，请不要关闭本页面！');
                    backup(res.data.tab, 0);
                    window.onbeforeunload = function(){ return '正在备份数据库，请不要关闭！' }
                } else {
                    toast.error(res.msg);
                    $export.parent().children().removeClass("disabled");
                    $export.html('立即备份');
                    setTimeout(function(){
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled',false);
                    },1500);
                }
            },"json");
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(res){
                if(res.code == 200){
                    showmsg(tab.id, res.msg);

                    if(!$.isPlainObject(res.data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html('备份完成，点击重新备份');
                        window.onbeforeunload = function(){ return null }
                        return false;
                    }
                    console.log(res.data.tab);
                    backup(res.data.tab, tab.id != res.data.tab.id);
                } else {
                    updateAlert(res.msg,'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html('立即备份');
                    setTimeout(function(){
    	                $('#top-alert').find('button').click();
    	                $(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>
{/block}